package com.madou.dbtool.redis.panel.dataloader;

import com.madou.common.annotation.HideClass;
import com.madou.dbtool.common.dto.Complex;
import com.madou.dbtool.redis.manager.RedisSourceManager;
import com.madou.dbtool.ui.table.row.MObjectRow;
import com.madou.dbtool.ui.table.MRow;
import com.madou.dbtool.ui.table.loader.TableDataLoader;
import redis.clients.jedis.resps.Slowlog;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * redis慢日志加载器
 * @author miukoo
 */
@HideClass
public class RedisSlowLogTableDataLoader implements TableDataLoader {

    RedisSourceManager redisSourceManager;

    public RedisSlowLogTableDataLoader(RedisSourceManager redisSourceManager){
        this.redisSourceManager = redisSourceManager;
    }

    @Override
    public Complex<Integer, List<MRow>> load(Map<String, Object> searchKeyword, int page, int size) {
        List<Object> execute = (List<Object>) redisSourceManager.execute(-1,"slowlog get "+size);
        List<Slowlog> from = Slowlog.from(execute);
        List<MRow> collect = from.stream().map(slowlog -> {
            return new MObjectRow(slowlog);
        }).collect(Collectors.toList());
        return new Complex<Integer, List<MRow>>(execute.size(),collect);
    }

}
